package com.example.kotlinlearn.datasave

import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.edit
import com.example.kotlinlearn.BaseActivity
import com.example.kotlinlearn.R
import com.example.kotlinlearn.databinding.ActivityDataSaveBinding
import java.io.*
import java.lang.StringBuilder

class DataSaveActivity : BaseActivity() {

    private lateinit var binding:ActivityDataSaveBinding

    @SuppressLint("CommitPrefEdits", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_data_save)
        binding = ActivityDataSaveBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val saveContent = loadEditText()
        if (saveContent.isNotEmpty()){
            binding.editText.setText(saveContent)
            Toast.makeText(this,"数据恢复成功!",Toast.LENGTH_SHORT).show()
        }
        binding.textView.text = (Math.random()*10).toInt().toString()
        binding.button.setOnClickListener {
            val editor = getSharedPreferences("Data",Context.MODE_PRIVATE).edit()
            editor.putString("name","一枝蒿")
            editor.putBoolean("gender",true)
            editor.putInt("age",22)
            editor.putLong("s", 14353568496048)
            editor.putFloat("f",35353.64646f)
            editor.apply()
            //简化写法
            getSharedPreferences("1",Context.MODE_PRIVATE).edit{
                putFloat("a",242f)
                putString("b","简化写法")
            }
        }
        binding.button1.setOnClickListener {
            if(binding.textView.text.length>2) {
                binding.textView.text=""
            }else{
                val prefs = getSharedPreferences("Data",Context.MODE_PRIVATE)
                val name = prefs.getString("name","")
                val age = prefs.getInt("age",0)
                val s = prefs.getLong("s",0)
                binding.textView.text = "姓名:$name\n年龄:$age"
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        val editText = binding.editText.text.toString()
        saveEditText(editText)
    }

    private fun saveEditText(inputText:String){
        try {
            val output = openFileOutput("data",Context.MODE_PRIVATE)
            val writer = BufferedWriter(OutputStreamWriter(output))
            writer.use {
                it.write(inputText)
            }
        }catch (e:IOException){
            e.printStackTrace()
        }
    }

    private fun loadEditText():String{
        val content = StringBuilder()
        try {
            val input = openFileInput("data")
            val reader = BufferedReader(InputStreamReader(input))
            reader.use {
                reader.forEachLine {
                    content.append(it)
                }
            }
        }catch (e:IOException){
            e.printStackTrace()
        }
        return content.toString()
    }
}